class BlogController < ApplicationController
  before_filter :setup
  
  def index
    @article_pages, @articles = paginate :articles,
                                         :order => 'created_at DESC',
                                         :per_page => 10                                         
  end
  
  def category
    @article_pages, @articles = paginate :articles,
                                         :conditions => ["category_id=?, params[:id]"],
                                         :order => 'created_at DESC',
                                         :per_page => 10                                         
    render :action => 'index'
  end
  
  def tag
    @article_pages, @articles = paginate :articles,
      :select => "a.*",
      :conditions => ["t.tag_id=?", params[:id]],
      :joins => "as a inner join taggings as t on a.id = t.article_id",
      :order => 'a.created_at DESC',
      :per_page => 10
    render :action => 'index'
  end
  
  def article
    @article = Article.find(params[:id])
    @comment = Comment.new
  end
  
  def comment
    @article = Article.find(params[:id])
    @comment = Comment.new(params[:comment])
    @comment.article = @article
    if @comment.save
      redirect_to :action => 'article', :id => @article
    else
      render :action => 'article'
    end
  end
  
  private
  def setup
    @categories = Category.find(:all)
    @tags = Tag.find(:all)
  end
end
